<?php

require_once '../include/common.inc.php';
include_once '../include/cart.class_p.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

//ɲ룬£ɾȲ?
if($continue!="")header("localtion: c_order_p.php"); 

$menu_index = 2;
$page_title = 'TABLA';
$guide_message = 'Customer | Order Information | Promoter';
$page_url = 'c_order_p.php?';
$ppp = 20;

getMyParam('action');
getMyParam('search_key');

if($action!=""){
	
	getMyParam('cid');	

if ($action =="edit") {
		getMyParam('deliver_status');
		getMyParam('cur_status');
		if(isset($deliver_status) && $deliver_status!="")
		{
			$sql="update `promoter_order` SET `deliver_status` = '$deliver_status' where id=$cid ";
			$db->query($sql);
			/////////////update stock data ---leo
			if($deliver_status!=$cur_status)
			{
				$sql="select * from promoter_order where id=$cid";
				$query = $db->query($sql);
				$order_ary = $db->fetch_array($query);
				$cart_info=new cart;
				$cart_info=unserialize($order_ary['cart_info']);
				$myCart = $cart_info->getData();
				$sql_update="";
				if($deliver_status=="2"&&$cur_status!="2")//restore stock data ---leo
				{		
					foreach($myCart as $key=>$val)
					{
						if($key!="")
						{
							if($val["count"]>0 && $val["type"]!="Courses / Programs")
							{
								$sql_update="update pro_product set stock_data=stock_data+".$val["count"]." where id=".$key;
								$mysql->query($sql_update);
							}
						}
					}
				}
				if($deliver_status!="2"&&$cur_status=="2")//update stock data ---leo
				{
					foreach($myCart as $key=>$val)
					{
						if($key!="")
						{
							if($val["count"]>0 && $val["type"]!="Courses / Programs")
							{
								$sql_update="update pro_product set stock_data=stock_data-".$val["count"]." where id=".$key." and stock_data>0";
								$mysql->query($sql_update);
							}
						}
					}
				}
			}
		}
		////////////////
		$msg="Successfully updated Order info.";
		$href="c_order_p.php";
		include_once('menu_list.php');
		require_once PrintEot('a_header');
		require_once PrintEot('a_msg');
		require_once PrintEot('a_footer');
		exit;
} 

if ($action == "del") {
    if (is_numeric($cid) && $cid > 0) {
        $sql = "DELETE FROM `promoter_order` WHERE `id` = '" . $cid . "'";
        $db->query($sql);
		$msg="This Order has been deleted. ";
		$href="c_order_p.php";
		include_once('menu_list.php');
		require_once PrintEot('a_header');
		require_once PrintEot('a_msg');
		require_once PrintEot('a_footer');
		exit;
    }
}

}

$sql_where = '';
if ($action == "search") {
	getMyParam('day1');
	getMyParam('day2');
	getMyParam('no1');
	getMyParam('no2');
	getMyParam('promoter');	
	getMyParam('deliver_status');
	//
	//ڼID
	if($day1!="") $day1=" FLOOR(date/(3600*24))>=".intval(strtotime($day1)/(3600*24))." and ";
	if($day2!="") $day2=" FLOOR(date/(3600*24))<=".intval(strtotime($day2)/(3600*24))." and ";
	if($no1!="")  $no1=" id>='".$no1."' and ";
	if($no2!="")  $no2=" id<='".$no2."' and ";
	
	//Promoter ID/Name
	if($promoter!=""){
	$sql = "SELECT id FROM  promoter_info  where id='".$promoter."' or firstname='".$promoter."' or lastname='".$promoter."' or concat(firstname,lastname)='".	$promoter."'";
	$query = $db->query($sql);
	$rs = $db->fetch_array($query);
	if($rs['id']!="")$promoter=" promoter_id=".$rs['id']." and ";else $promoter=" 1<0 and ";
	}
	

	//Status
	if($deliver_status=="all") $deliver_status="";
	else $deliver_status=" deliver_status=".$deliver_status." and ";
	
    if($day1!=""||$day2!=""||$no1!=""||$no2!=""||$promoter!=""||$customer!=""||$deliver_status!="all")
	 $sql_where = "where ".$no1.$no2.$day1.$day2.$promoter.$customer.$deliver_status." 1>0 ";
}



//
$sql = "SELECT `id` FROM `promoter_order` " . $sql_where;
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);

// *** page code start ***
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}
// *** page code end ***

$sql = "SELECT * FROM  promoter_order  ".$sql_where. "ORDER BY `id` DESC LIMIT $start_limit, $ppp";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $out_ary[] = $rs;
}


for($i=0;$i<count($out_ary);$i++)
{

if($out_ary[$i]['card_type']=="V-DK" || $out_ary[$i]['card_type']=="VISA")$out_ary[$i]['new_id']=$out_ary[$i]['id']."<font color=red>*</font>";
else $out_ary[$i]['new_id']=$out_ary[$i]['id'];

$out_ary[$i]['date']=date('Y-m-d',$out_ary[$i]['date']);

//$sql = "SELECT * FROM  cus_members  where id=".$out_ary[$i]['user_id'];
//$query = $db->query($sql);
//$rs = $db->fetch_array($query);
//$out_ary[$i]['customer']=$rs['firstname'].$rs['lastname'];

$sql = "SELECT * FROM  promoter_info  where id=".$out_ary[$i]['promoter_id'];
$query = $db->query($sql);
$rs = $db->fetch_array($query);
$out_ary[$i]['promoter']=$rs['id']."/".$rs['firstname'].$rs['lastname'];

$out_ary[$i]['cost']=number_format($out_ary[$i]['cost'],2);

if($out_ary[$i]['pay_flag']==0)
{
	if($out_ary[$i]['deliver_status']==0)$out_ary[$i]['deliver_status']="Non-deliver";
	elseif($out_ary[$i]['deliver_status']==1) $out_ary[$i]['deliver_status']="Delivered";
	else $out_ary[$i]['deliver_status']="Cancelled";
}
else
{
	$out_ary[$i]['deliver_status']="Aborted";
}

}

$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);

include_once('menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_c_order_p');
require_once PrintEot('a_footer');

?>